local BaseData = require("data.base.BaseData")
local BattleData = class("BattleData", BaseData)
local StoreConst = require("const.StoreConst")

local schema = {}
BattleData.schema = schema

local MAX_BATTLE_HERO = 6

function BattleData:ctor()
    BattleData.super.ctor(self)
end

function BattleData:init()
    self:_initData()
end

function BattleData:save()
    local result = {}
    for pos, equipId in pairs(self._battleEquipment) do
        local param = {}
        param.pos = pos
        param.equipId = equipId
        table.insert(result, param)
    end
    G_StoreManager:setDataList(StoreConst.BATTLE_EQUIP, result)
    G_StoreManager:setDataList(StoreConst.BATTLE_HEROS, self._battleHero)
end

function BattleData:_initData()
    self._battleEquipment = {}
    self._battleHero = {}
    self._battleHeroMaps = {}
    local list = G_StoreManager:getDataList(StoreConst.BATTLE_EQUIP) or {}
    for i = 1, #list do
        self._battleEquipment[list[i].pos] = list[i].equipId
    end
    local list = G_StoreManager:getDataList(StoreConst.BATTLE_HEROS)
    if list then
        self._battleHero = list
    end
    for i = 1, #self._battleHero do
        local heroId = self._battleHero[i]
        local unit = G_UserData:getHero():getUnitById(heroId)
        self._battleHeroMaps[heroId] = {pos = i, data = unit}
    end
end

function BattleData:_isExistEquipment(equipId)
end

function BattleData:addEquipmentToBattle(pos, equipId)
    if not self._battleHero[pos] then
        return
    end
    for i, id in pairs(self._battleEquipment) do
        if id == equipId then
            self._battleEquipment[i] = nil
        end
    end
    self._battleEquipment[pos] = equipId
    G_SignalManager:dispatch(SignalConst.EVENT_EQUIPMENT_ADD_SUCCESS)
end

function BattleData:clearEquipmentInBattle(pos)
    self._battleEquipment[pos] = nil
    G_SignalManager:dispatch(SignalConst.EVENT_EQUIPMENT_CLEAR_SUCCESS)
end

function BattleData:clearHeroInBattle(heroId)
    local index = 0
    for i = 1, #self._battleHero do
        if self._battleHero[i] == heroId then
            table.remove(self._battleHero, i)
            self._battleHeroMaps[heroId] = nil
            self._battleEquipment[i] = nil
            index = i
            break
        end
    end
    if index > 0 then
        for i = index + 1, MAX_BATTLE_HERO do
            if self._battleEquipment[i] then
                self._battleEquipment[i - 1] = self._battleEquipment[i]
                self._battleEquipment[i] = nil
            end
        end
    end
    G_SignalManager:dispatch(SignalConst.EVENT_HERO_UN_BATTLE_SUCCESS)
end

function BattleData:addHeroToBattle(heroId)
    if #self._battleHero == MAX_BATTLE_HERO then
        return false
    end
    table.insert(self._battleHero, heroId)
    local unit = G_UserData:getHero():getUnitById(heroId)
    self._battleHeroMaps[heroId] = {pos = #self._battleHero, data = unit}
    G_SignalManager:dispatch(SignalConst.EVENT_HERO_ADD_BATTLE_SUCCESS)
    return true
end

function BattleData:changeHeroToBattle(pos, heroId)
    if not self._battleHero[pos] then
        return
    end
    local oldHeroId = self._battleHero[pos]
    self._battleHeroMaps[oldHeroId] = nil
    self._battleHero[pos] = heroId
    self._battleHeroMaps[heroId] = {pos = pos, data = G_UserData:getHero():getUnitById(heroId)}
    G_SignalManager:dispatch(SignalConst.EVENT_HERO_CHANGE_BATTLE_SUCCESS)
end

function BattleData:getBattleEquipment(pos)
    local equipId = self._battleEquipment[pos]
    if equipId then
        local unit = G_UserData:getEquipment():getUnitById(equipId)
        return unit
    end
end

function BattleData:getBattleHeros()
    local result = {}
    for i = 1, #self._battleHero do
        local unit = G_UserData:getHero():getUnitById(self._battleHero[i])
        table.insert(result, unit)
    end
    return result
end

function BattleData:isInBattleHero(heroId)
    if self._battleHeroMaps[heroId] then
        return true
    end
    return false
end

function BattleData:isInBattleEquipment(equipId)
    for i = 1, #self._battleEquipment do
        if self._battleEquipment[i] == equipId then
            return true
        end
    end
    return false
end

function BattleData:getHeroBattlePos(heroId)
    local heroData = self._battleHeroMaps[heroId]
    if heroData then
        return heroData.pos
    end
    return 0
end

return BattleData
